package com.cloudfast.common.fence;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子围栏越界判断
 *
 * @author liuyw
 * @date 2016-12-16
 */
public class FenceCrossingUtil {

    /**
     * 判断点是否内list 围栏内
     *
     * @param p    需要判断的点
     * @param list 围栏数组
     * @return
     */
    public boolean fenceCross(Point p, List<Point> list) {
        Point2D.Double point = new Point2D.Double(p.getLongitude(),
                p.getLatitude());
        List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
        for (Point pt : list) {
            Point2D.Double pp = new Point2D.Double(pt.getLongitude(),
                    pt.getLatitude());
            pointList.add(pp);
        }

        GeneralPath gpath = new GeneralPath();
        Point2D.Double first = pointList.get(0);
        gpath.moveTo(first.x, first.y);
        for (Point2D.Double d : pointList) {
            gpath.lineTo(d.x, d.y);
        }
        gpath.lineTo(first.x, first.y);
        gpath.closePath();
        return gpath.contains(point);

    }

    /**
     * 测试
     *
     * @param lng
     * @param lat
     * @return
     */
    public boolean getTest(double lng, double lat) {
        List<Point> list = new ArrayList<Point>();
        list.add(new Point(117.092348, 36.645482));
        list.add(new Point(117.104082, 36.64701));
        list.add(new Point(117.105003, 36.641835));
        list.add(new Point(117.097447, 36.637398));
        list.add(new Point(117.096034, 36.644201));
        list.add(new Point(117.092348, 36.645482));
        Point p = new Point(lng, lat);

        FenceCrossingUtil m = new FenceCrossingUtil();
        boolean b = m.fenceCross(p, list);
        return b;
    }

}

